#include <fstream>
#include <iostream>
#include <string>
#include <stack>
#include "UnlimitedNumber.h"
using namespace std;
UnlimitedNumber calculate(UnlimitedNumber const & a, char c, UnlimitedNumber const & b)
{
   UnlimitedNumber answ;
   if(c == '+')
      answ = a + b;
   else if (c == '-')
      answ = a - b;
   else if (c == '*')
      answ = a * b;
   else if (c == '/')
      answ = a / b;
   else
      answ = -666;
   return answ;
}

/*VL use this style, not Java-style
void foo()
{  
   // bracer on the next line 
}
*/
//SNMA: OK.

int main()
{
   ifstream fi("in.txt");
   ofstream fo("out.txt");
   string str;
   while(getline(fi, str)){
      str.erase(0, str.find_first_not_of("\n\t\r "));
      str.erase(str.length() - 1, 1);
      string output;
      cout << str << " = ";
      stack <UnlimitedNumber> numbers;
      try
      {
         while (str != "")
         {
            if( (str.find_first_not_of("+-*/") > 0) && ((str.length() == 1) || str[1] < '0' || str[1] > '9') )
            {
               char operation = str[0];
               str.erase(0, str.find_first_not_of("+-*/"));
               if(numbers.size() < 2)
               {
                  output += "incorrect input line!\n";
                  break;
               }
               else{
                  UnlimitedNumber u = numbers.top();
                  numbers.pop();
                  u = calculate(numbers.top(), operation, u);
                  numbers.pop();
                  numbers.push(u);
               }
            }
            else{
               numbers.push((UnlimitedNumber)str.substr(0, str.find_first_not_of("-0123456789")));
               str.erase(0, str.find_first_not_of("-0123456789"));
            }

            str.erase(0, str.find_first_not_of("\n\t\r "));
         }
         output += numbers.top().get();
      }
      catch (const char * err)
      { output = err; }
      fo << output << "\n";
      cout << output << "\n";
   }
   fi.close();
   fo.close();
   return 0;
}
